package com.jxufe.web;

import com.jxufe.entity.Blog;
import com.jxufe.entity.Forum;
import com.jxufe.entity.User;
import com.jxufe.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class ForumShowController {

    @Resource
    private ForumService forumService;

    @Autowired
    private TagService tagService;

    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private CommentService commentService;


    @GetMapping("/forum")
    public String doForum(HttpServletRequest request, Model model) {
        request.getSession().setAttribute("lastPath", request.getServletPath());

        model.addAttribute("forums", forumService.listForums());
        model.addAttribute("tags", tagService.listTag());
        if (request.getSession().getAttribute("user") != null) {
            return "user_forum";
        }
        return "forum";
    }

    @GetMapping("/user_forum")
    public String doUserForum(HttpServletRequest request, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        model.addAttribute("forums", forumService.listForums());
        model.addAttribute("tags", tagService.listTag());
        model.addAttribute("joinForums", forumService.findJoinForums(user.getId()));
        model.addAttribute("notJoinForums", forumService.findNotJoinForums(user.getId()));
        return "user_forum";
    }

    @GetMapping("/forum/fblog/{id}")
    public String doFblog(@PageableDefault(size = 999, sort = {"updateTime"}, direction = Sort.Direction.DESC)
                          Pageable pageable, @PathVariable Integer id, HttpServletRequest request, Model model) {
        request.getSession().setAttribute("lastPath", request.getServletPath());

        model.addAttribute("forum", forumService.getForumById(id));
        model.addAttribute("tags", tagService.listTag());
        model.addAttribute("hotBlogs", blogService.getHotBlog());
        model.addAttribute("blogs", blogService.findBlogs(id));
        if (request.getSession().getAttribute("user") != null) {
            return "user_fblog";
        }
        return "fblog";
    }

    @GetMapping("/user_forum/fblog/{id}")
    public String doUserFblog(@PageableDefault(size = 999, sort = {"updateTime"}, direction = Sort.Direction.DESC)
                              @PathVariable Integer id, HttpServletRequest request, Model model) {
        model.addAttribute("forum", forumService.getForumById(id));
        model.addAttribute("tags", tagService.listTag());
        model.addAttribute("hotBlogs", blogService.getHotBlog());
        model.addAttribute("blogs", blogService.findBlogs(id));
        return "user_fblog";
    }

    @GetMapping("/forum/fblog/{forumId}/{tagId}")
    public String doFblogByTag(@PageableDefault(size = 999, sort = {"updateTime"}, direction = Sort.Direction.DESC)
                               @PathVariable Integer forumId, @PathVariable Long tagId, HttpServletRequest request, Model model) {
        request.getSession().setAttribute("lastPath", request.getServletPath());

        model.addAttribute("forum", forumService.getForumById(forumId));
        model.addAttribute("tags", tagService.listTag());
        model.addAttribute("hotBlogs", blogService.getHotBlog());
        model.addAttribute("blogs", blogService.findBlogsByTag(forumId, tagId));
        model.addAttribute("activeTagId", tagId);
        if (request.getSession().getAttribute("user") != null) {
            return "user_fblog_tags";
        }
        return "fblog_tags";
    }

    @GetMapping("/user_forum/fblog/{forumId}/{tagId}")
    public String doUserFblogByTag(@PageableDefault(size = 999, sort = {"updateTime"}, direction = Sort.Direction.DESC)
                                   @PathVariable Integer forumId, @PathVariable Long tagId, Model model) {
        model.addAttribute("forum", forumService.getForumById(forumId));
        model.addAttribute("tags", tagService.listTag());
        model.addAttribute("hotBlogs", blogService.getHotBlog());
        model.addAttribute("blogs", blogService.findBlogsByTag(forumId, tagId));
        model.addAttribute("activeTagId", tagId);
        return "user_fblog_tags";
    }

    @GetMapping({"/user_forum/join", "/forum/join"})
    public void doForumJoin(Integer userId, Integer forumId, HttpServletResponse response) throws IOException {
        response.setContentType("text/html; charset=UTF-8");
        Integer res = forumService.joinForum(userId, forumId);
        if (res == 1) {
            forumService.updatePeopleNum(forumId);
            response.getWriter().print("<script language='javascript'>alert('添加成功');" +
                    "window.location.href='/dev/user_forum';</script>");
        } else {
            response.getWriter().print("<script language='javascript'>alert('添加失败');" +
                    "window.location.href='/dev/user_forum';</script>");
        }
    }

    @GetMapping({"/user_forum/exit"})
    public void doForumExit(Integer userId, Integer forumId, HttpServletResponse response) throws IOException {
        response.setContentType("text/html; charset=UTF-8");
        Integer res = forumService.exitForum(userId, forumId);
        if (res == 1) {
            response.getWriter().print("<script language='javascript'>alert('退出成功');" +
                    "window.location.href='/dev/user_forum';</script>");
        } else {
            response.getWriter().print("<script language='javascript'>alert('退出失败');" +
                    "window.location.href='/dev/user_forum';</script>");
        }
    }

    @GetMapping("/fblog/input")
    public String doWriteBlog(Model model) {
        model.addAttribute("forums", forumService.listForums());
        model.addAttribute("tags", tagService.listTag());
        return "fblog-input";
    }

    @GetMapping("/fblog/edit/{id}")
    public String doEditBlog(@PathVariable Long id, Model model) {
        model.addAttribute("blog", blogService.getBlog(id));
        model.addAttribute("forums", forumService.listForums());
        model.addAttribute("tags", tagService.listTag());
        return "fblog-input";
    }

    @PostMapping("/fblog/input/success")
    public void doWriteBlog(Blog blog, HttpSession session, HttpServletResponse response) throws IOException {
        if (blog.getContent().equals("") || blog.getDescription().equals("") ||
                blog.getTitle().equals("") || blog.getForum() == null || blog.getTag() == null) {
            response.getWriter().print("<script language='javascript'>alert('内容请填写完整');" +
                    "window.location.href='/dev/fblog/input';</script>");
        } else {
            User user = (User) session.getAttribute("user");
            blog.setUser(user);

            if (blog.getId() == null) {
                blog = blogService.saveBlog(blog);
                forumService.updateBlogNum(blog.getForum().getId());
                userService.updateBlogNum(user.getId());
                forumService.updateHotById(Long.valueOf(blog.getForum().getId()));
            } else {
                blog = blogService.updateBlog(blog.getId(), blog);
                userService.updateCollectionByBlog(blog.getId(), blog.getTitle());
            }
            response.sendRedirect("/dev/user_blog/" + blog.getId());
        }
    }

    @PostMapping("/fblog/delete/{blogId}")
    @Transactional
    public String deleteBlog(@PathVariable Long blogId, @RequestParam(value = "forumId", required = false) Integer forumId) {
        commentService.deleteCommentByBlog(blogId);
        blogService.deleteBlog(blogId);
        blogService.removeCollection(blogId);
        forumService.reduceBlogNum(forumId);
        return "redirect:/archives";
    }
}
